<template>
	<view>
		<view class="header-wrpa">
			<view class="header-title">
				<view class="en">
					welcome
				</view>
				<view class="zh">
					欢迎登录AI期货通
				</view>
			</view>
		</view>
		<view class="logon-form">
			<view class="form-title">
				登录
			</view>
			<view class="form-row">
				<uni-data-select v-model="current" :localdata="range" placeholder="请选择细微"
					@change="change"></uni-data-select>
			</view>


			<view class="form-row">
				<uni-easyinput class="form-input" :inputBorder="false" primaryColor="#C2C2C2" trim="all"
					v-model="form.account_no" placeholder="请输入账号"></uni-easyinput>
			</view>
			<view class="form-row">
				<uni-easyinput type=password :inputBorder="false" primaryColor="#C2C2C2" passwordIcon class="form-input"
					trim="all" v-model="form.account_pw" placeholder="请输入密码"></uni-easyinput>
			</view>

			<!-- <view class="form-function">
				<view class="remember">
					<view class="option" @click="isRemember = !isRemember">
						<image v-if="isRemember" class="img-full" src="@/static/logon/selected.png"></image>
						<view v-else class="null"></view>
					</view>
					<text>记住密码</text>
					<text class="col-green" @click="register">没有账号去注册</text>
				</view>
				<view class="forget" @click="toFind">
					忘记密码?
				</view>
			</view> -->

			<view class="user-agreement">
				<view class="option" @click="isAgreement = !isAgreement">
					<image v-if="isAgreement" class="img-full" src="@/static/logon/selected.png"></image>
					<view v-else class="null"></view>
				</view>
				已阅读并同意<text @click="toPrivacyPolicy">《用户协议》</text><text @click="toPrivacyPolicy">《支付协议》</text><text
					@click="toPrivacyPolicy">《隐私权政策》</text>
			</view>
			<view class="logon-but" @click="login">
				登录
			</view>
		</view>
	</view>
</template>

<script>
	import {
		login
	} from "@/utils/utils.js";
	import Socket from '@/utils/ws.js'
	import Socket1 from '@/utils/ws1.js'

	export default {
		data() {
			return {
				current: 0,
				range: [{
					value: 0,
					text: "模拟席位",
					id: 'LTLH',
					url: 'ws://124.220.157.145:11132'
				}],
				form: {
					account_no: "J1001",
					account_pw: "J1001",
				},
				isRemember: false,
				isAgreement: false
			};
		},
		onShow() {
			this.getStorage();
		},
		methods: {
			change(e) {
				console.log(e, '===>e', this.current, '000')
			},
			toPrivacyPolicy() {
				uni.navigateTo({
					url: '/pages/user/privacyPolicy/privacyPolicy'
				});
			},
			toFind() {
				uni.navigateTo({
					url: '/pages/logon/find/find'
				});
			},
			register() {
				uni.navigateTo({
					url: '/pages/logon/register/register'
				});
			},
			getStorage() {
				var phone = uni.getStorageSync('phone');
				var code = uni.getStorageSync('password');
				if (!!code) {
					this.isRemember = true;
					this.form.code = code;
					if (!!phone) this.form.phone = phone;
					else this.form.phone = '';
				} else this.form.code = '';
			},
			// 登录内容
			login() {
				var that = this;
				if (!this.form.account_no) {
					uni.showToast({
						title: "请输入账号",
						icon: "none",
						duration: 2000,
					});
					return;
				} else if (!this.form.account_pw) {
					uni.showToast({
						title: "请输入密码",
						icon: "none",
						duration: 2000,
					});
					return;
				} else if (!this.isAgreement) {
					uni.showToast({
						title: "请勾选用户协议",
						icon: "none",
						duration: 2000,
					});
					return;
				}

				Socket.start({
					// url: 'ws://124.220.157.145:11132',s
					url: this.range[this.current].url,
					socketKey: 'login'
				})

				var Td = {
					chk: "0",
					account_id: 1,
					account_no: "J1001",
					account_pw: "J1001",
					broker_id: this.range[this.current].id,
					broker_name: this.range[this.current].text,
					is_login: 0,
					status: "0",
					remark: null,
					type: "0",
					day: "0",
					user_name: null,
					real_name: null,
					pid: 0,
					dc: 0,
					mul: 0.0,
					ttype: 0,
					op: 0
				}

				Socket.init((res) => {
					// console.log('收到消息：', res.data);
					var arr = res.data.split('|');
					// console.log(arr, '====>arr')
					var type = arr[0];
					if (arr.Length < 2) {
						MsgManager.Instance.SendMsg(_Name + "接收数据错误:" + pData, MsgType.ERROR);
						return;
					}
					if (type == "H") //心跳
					{
						consle.log('h', '登录返回')

					} else if (type == "L") //登录返回
					{
						var Td = JSON.parse(arr[1]);
						console.log(Td, '===>登录返回')

						that.checkStatus()
						// 返回界面
						// uni.navigateBack()

					} else if (type == "LO") //登出返回
					{
						var Td = JSON.parse(arr[1]);
						console.log(Td, '===>退出返回')

					} else if (type == "O") //报单
					{
						var orderInfo = JSON.parse(arr[1]);
						console.log(orderInfo, '===>报单')

					} else if (type == "C") //撤单回报
					{
						var orderInfo = JSON.parse(arr[1]);
						console.log(Td, '===>撤单回报')


					} else if (type == "T") //成交
					{
						var tradeInfo = JSON.parse(arr[1]);
						console.log(Td, '===>成交')

					} else if (type == "P") //持仓
					{
						var poInfo = JSON.parse(arr[1]);
						console.log(poInfo, '===>持仓', )

						// that.$nextTick(() => {
						// 	console.log(that.$refs.xx, '=======>xx')
						// 	that.$refs.imitate.chooseTableData(poInfo)
						// })


					} else if (type == "PL") //持仓列表
					{
						Poss = JSON.parse(arr[1]);
						console.log(Td, '===>持仓列表')
					} else if (type == "M") //权益
					{
						var amountInfo = JSON.parse(arr[1]);
						console.log(amountInfo, '===>权益')
					} else if (type == "E") //错误
					{
						uni.showToast({
							title: "服务器返回错误:" + arr[1],
							icon: 'error'
						})
					}

				}, ('L|' + JSON.stringify(Td)))

				// login(this.form.phone, this.form.code).then((res) => {
				// 	uni.setStorageSync('phone', this.form.phone);
				// 	// uni.setStorageSync('token', res.token);
				// 	if (this.isRemember) uni.setStorageSync('password', this.form.code);
				// 	// console.log(res);
				// }, err => {
				// 	console.log(err);
				// });
			},

			// 登录状态
			checkStatus() {
				Socket1.start({
					url: 'ws://124.220.157.145:11126',
					socketKey: 'login1'
				})
				Socket1.init((res) => {
					console.log(res, '=======000')
					var arr = res.data.split('|');
					console.log(arr, '====>arr')
					if (arr[0] == 'A') {
						Socket1.socketTask['login1'].send({
							data: ('ISS|1'),
							success: () => {
								console.log("发送websocket中...");
							},
							fail: (err) => {

							}
						})
						uni.reLaunch({
							url: '/pages/tabBar/transaction/transaction'
						})
					}



				}, 'A|' + JSON.stringify({
					name: "",
					pw: ""
				}))
			}

		},
	}
</script>

<style lang="scss">
	page {
		background-color: #F5F7FB;
	}

	.col-green {
		color: #356FE5;
		padding-left: 30rpx;
		font-size: 24rpx;
	}

	.header-wrpa {
		width: 750rpx;
		height: 634rpx;
		background-image: url('@/static/logon/headerBack.png');
		background-repeat: no-repeat;
		background-size: 100% 100%;
		box-sizing: border-box;
		padding-top: 196rpx;
		padding-left: 64rpx;

		.header-title {
			.en {
				font-size: 64rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				color: #FFFFFF;
			}

			.zh {
				font-size: 36rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				color: #FFFFFF;
				margin-top: 10rpx;
			}
		}
	}

	.logon-form {
		width: 686rpx;
		height: 792rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		padding: 48rpx 32rpx;
		margin: 0 auto;
		box-sizing: border-box;
		transform: translateY(-190rpx);

		.form-title {
			font-size: 40rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: bold;
			color: #333333;
		}

		.form-row {
			margin-top: 48rpx;
		}

		.form-input {
			width: 622rpx;
			height: 108rpx;
			border-radius: 16rpx;
			opacity: 1;
			border: 2rpx solid #ECECEC;
			display: flex;
			align-items: center;
		}

		.option {
			width: 32rpx;
			height: 32rpx;
			display: flex;
			align-items: center;
			justify-content: center;
			margin: 10rpx;

			.null {
				width: 28rpx;
				height: 28rpx;
				border-radius: 50%;
				border: 2rpx solid #333333;
			}
		}

		.form-function {
			margin-top: 32rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.remember {
				display: flex;
				align-items: center;
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: rgba(0, 0, 0, 0.4);
			}

			.forget {
				font-size: 28rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				color: rgba(0, 0, 0, 0.4);
			}
		}

		.user-agreement {
			margin-top: 66rpx;
			text-align: center;
			display: flex;
			justify-content: center;
			font-size: 20rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: #333333;
			align-items: center;

			text {
				color: #EE5953;
			}
		}

		.logon-but {
			width: 622rpx;
			height: 92rpx;
			background: #EE5953;
			border-radius: 44rpx;
			font-size: 32rpx;
			font-family: PingFang SC, PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
			line-height: 92rpx;
			text-align: center;
			margin-top: 32rpx;
		}
	}
</style>